package com.example.lcqexcel.yuexiu.pdLIb;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EasyExcelUtils
 * @Description
 * @Author lichuanqi
 * @Date 2023/8/11 14:18
 * @Version 1.0
 **/
public class EasyExcelUtils {
    public static void main(String[] args) {
        String filePath = "d:\\test\\yuexiu\\pdlib\\a.xlsx";
        List<ExcelHead> headList = new ArrayList();
        headList.add(new ExcelHead<String>("name", "名称"));
        headList.add(new ExcelHead("age", "年龄", -1));
        WriteSheet sheet = new WriteSheet();
        List<Map<String, Object>> dataList = new ArrayList();
        Map<String, Object> one = new HashMap();
        one.put("name", "张三");
        one.put("age", 20);
        dataList.add(one);

        Map<String, Object> two = new HashMap();
        two.put("name", "李四");
        two.put("age", 18);
        dataList.add(two);

        Map<String, Object> there = new HashMap();
        there.put("name", "不知道年龄");
        there.put("age", null);
        dataList.add(there);
        EasyExcelUtils.write(filePath, headList, dataList);
    }

    /**
     * 写excel
     *
     * @param filePath 保存的路径名
     * @param headList
     * @param dataList
     */
    public static void write(String filePath,
                             List<ExcelHead> headList,
                             List<Map<String, Object>> dataList) {
        ExcelWriterBuilder writerBuilder = EasyExcel.write();
        writerBuilder.file(filePath);
        writerBuilder.excelType(ExcelTypeEnum.XLSX);
        writerBuilder.autoCloseStream(true);

        writerBuilder.head(convertHead(headList)).sheet("sheet1")
                .doWrite(convertData(headList, dataList));
    }


    /**
     * 会先删除excel所有sheet，再写入
     */
    public static <T> void writeSheet(String filePath, String sheetName, Class<T> c, List<T> list) {
        EasyExcel.write(filePath, c).sheet(sheetName).doWrite(list);
    }
    public static <T> List<T> read(String fileName, String sheetName, Class c) {

        List<T> list = new ArrayList();
        EasyExcel.read(fileName, c, new ReadListener<T>() {
            @Override
            public void invoke(T o, AnalysisContext analysisContext) {
                list.add(o);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }).sheet(sheetName).doRead();
        return list;
    }



    private static List<List<String>> convertHead(List<ExcelHead> headList) {
        List<List<String>> list = new ArrayList<>();
        for (ExcelHead head : headList) {
            list.add(Lists.newArrayList(head.getTitle()));
        }
        //沒有搞清楚head的参数为List<List<String>>,用List<String>就OK了
        return list;
    }

    /**
     * @param headList
     * @param dataList key为head里的fieldName
     * @return
     */
    private static List<List<Object>> convertData(List<ExcelHead> headList, List<Map<String, Object>> dataList) {
        List<List<Object>> result = new ArrayList();
        //对dataList转为easyExcel的数据格式
        for (Map<String, Object> data : dataList) {
            List<Object> row = new ArrayList();
            for (ExcelHead h : headList) {
                Object o = data.get(h.getFieldName());
                //需要对null的处理，比如age的null，要转为-1
                row.add(handler(o, h.getNullValue()));
            }
            result.add(row);
        }
        return result;
    }

    /**
     * null值处理
     *
     * @param o
     * @param nullValue
     * @return
     */
    private static Object handler(Object o, Object nullValue) {
        return o != null ? o : nullValue;
    }
}
